package com.wjk.kylin.modules.system.role.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjk.kylin.modules.system.role.entity.RoleMenu;
import com.wjk.kylin.modules.system.role.mapper.RoleMenuMapper;
import com.wjk.kylin.modules.system.role.service.IRoleMenuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 角色和菜单关联表 服务实现类
 * </p>
 */
@Service
@Transactional(readOnly=true)
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements IRoleMenuService {


    @Override
    @Transactional(readOnly=false)
    public void deleteByRoleIds(List<String> roleIds) {
        QueryWrapper<RoleMenu> wrapper = new QueryWrapper<>();
        wrapper.in("role_id", roleIds);
        super.remove(wrapper);
    }

    @Override
    @Transactional(readOnly=false)
    public void deleteByMenuIds(List<String> MenuIds) {
        QueryWrapper<RoleMenu> wrapper = new QueryWrapper<>();
        wrapper.in("menu_id", MenuIds);
        super.remove(wrapper);
    }

    @Override
    @Transactional(readOnly=false)
    public void save(String roleId, List<String> menuIds) {
        List<RoleMenu> roleMenuList = new ArrayList<>();
        menuIds.forEach(menuId->{
            RoleMenu roleMenu = new RoleMenu();

            roleMenu.setMenuId(menuId);
            roleMenu.setRoleId(roleId);

            roleMenuList.add(roleMenu);
        });

        super.saveBatch(roleMenuList);
    }

    @Override
    public List<String> findMenuListByRoleId(String roleId) {
        QueryWrapper<RoleMenu> wrapper = new QueryWrapper<>();
        wrapper.in("role_id", roleId);

        List<RoleMenu> list = super.list(wrapper);

        return list.stream().map(RoleMenu::getMenuId).distinct().collect(Collectors.toList());
    }
}
